package cn.drh.charging.order.dao.repository.impl;


import cn.drh.charging.order.constant.OrderStatusConstant;
import cn.drh.charging.order.dao.mapper.OrderFailMapper;
import cn.drh.charging.order.dao.mapper.OrderSuccessMapper;
import cn.drh.charging.order.dao.repository.OrderRepository;
import cn.drh.charging.order.pojo.po.ChargingBillFailPO;
import cn.drh.charging.order.pojo.po.ChargingBillSuccessPO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * @ClassName OrderRepositoryImpl
 * @Description TODO
 * @Author renhong~Deng
 * @Date 2025/6/4 16:12
 * @Version 1.0
 */

@Repository("orderRepository")
public class OrderRepositoryImpl implements OrderRepository {
    @Autowired
    private OrderSuccessMapper orderSuccessMapper;

    @Autowired
    private OrderFailMapper orderFailMapper;

    @Override
    public ChargingBillSuccessPO getSuccessOrder(String orderNo) {
        QueryWrapper<ChargingBillSuccessPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bill_id", orderNo);
        //queryWrapper 查询的条件
        //where bill_id = orderNo
        return orderSuccessMapper.selectOne(queryWrapper);

    }

    @Override
    public void updateOrderStatusFromProcess2ExceptionEnd(String orderNo) {
        //把状态是 正在充电中的 唱歌订单数据  的状态 修改为异常结束
        //update charging_order set status = 3 where order_no = orderNo and bill_status=1

        //更新的目标值bill_status=3(异常结束）
        ChargingBillSuccessPO chargingBillSuccessPO = new ChargingBillSuccessPO();
        chargingBillSuccessPO.setBillStatus(OrderStatusConstant.ORDER_STATUS_EXCEPTION);

        //UpdateWrapper更新的条件where bill_id = orderNo and bill_status=1
        UpdateWrapper<ChargingBillSuccessPO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("bill_id", orderNo);
        updateWrapper.eq("bill_status", OrderStatusConstant.ORDER_STATUS_PROGRESS);

    }

    @Override
    public ChargingBillFailPO getFailOrder(String orderNo) {
        QueryWrapper<ChargingBillFailPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bill_id", orderNo);
        return orderFailMapper.selectOne(queryWrapper);
    }

    @Override
    public void saveFailOrder(ChargingBillFailPO chargingBillFailPO1) {
        orderFailMapper.insert(chargingBillFailPO1);
    }

    /**
     * 更新设备信息
     * <p>
     * 此方法用于更新特定订单号对应的充电成功记录中的枪ID
     * 通过创建一个包含新枪ID的ChargingBillSuccessPO对象，并使用UpdateWrapper来指定更新条件（订单号）
     *
     * @param orderNo 订单号，用于定位需要更新的充电成功记录
     * @param gunId   新的枪ID，需要更新到充电成功记录中的枪ID字段
     */
    @Override
    public void updateDeviceInfo(String orderNo, Integer gunId) {
        // 创建一个ChargingBillSuccessPO对象，并设置新的枪ID
        ChargingBillSuccessPO chargingBillSuccessPO = new ChargingBillSuccessPO();
        chargingBillSuccessPO.setGunId(gunId);

        // 创建一个UpdateWrapper对象，用于构建更新条件
        UpdateWrapper<ChargingBillSuccessPO> updateWrapper = new UpdateWrapper<>();
        // 设置更新条件为订单号等于传入的orderNo
        updateWrapper.eq("bill_id", orderNo);

        // 调用orderSuccessMapper的update方法，更新符合条件的记录
        orderSuccessMapper.update(chargingBillSuccessPO, updateWrapper);
    }

    @Override
    public void saveSuccessOrder(ChargingBillSuccessPO chargingBillSuccessPO) {
        orderSuccessMapper.insert(chargingBillSuccessPO);
    }

}
